package com.hmkj.web.controller.my;

import com.hmkj.common.entity.ResultEntity;
import com.hmkj.core.constant.SessionId;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.model.user.UserModel;
import com.hmkj.core.po.user.User;
import com.hmkj.core.service.user.UserService;
import com.hmkj.web.controller.GenericController;
import com.xiaoleilu.hutool.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * ......................我佛慈悲......................
 * ......................_oo0oo_.......................
 * .....................o8888888o......................
 * .....................88" . "88......................
 * .....................(| -_- |)......................
 * .....................0\  =  /0......................
 * ...................___/`---'\___....................
 * ..................' \\|     |// '...................
 * ................/ \\|||  :  |||// \.................
 * .............../ _||||| -卍-|||||- \................
 * ..............|   | \\\  -  /// |   |...............
 * ..............| \_|  ''\---/''  |_/ |...............
 * ..............\  .-\__  '-'  ___/-. /...............
 * ............___'. .'  /--.--\  `. .'___.............
 * .........."" '<  `.___\_<|>_/___.' >' ""............
 * ........| | :  `- \`.;`\ _ /`;.`/ - ` : | |.........
 * ........\  \ `_.   \_ __\ /__ _/   .-` /  /.........
 * ....=====`-.____`.___ \_____/___.-`___.-'=====......
 * ......................`=---='.......................
 * ....................................................
 * .................佛祖开光 ,永无BUG...................
 */
@Api(description = "账户设置相关")
@Slf4j
@RestController
@RequestMapping(GenericController.MEMBER_URL + "/settings")
public class MyAccountSettingController extends GenericController<Integer, User> {
    @Resource
    private UserService userService;

    @ApiOperation(value = "个人中心-账号设置-修改登录密码", notes = "修改登录密码")
    @ResponseBody
    @RequestMapping(value = "editPwd", method = RequestMethod.POST)
    public ResultEntity editPwd(@ApiParam("当前密码") @RequestParam(required = true) String oldPwd,
                                @ApiParam("新密码") @RequestParam(required = true) String newPwd) {
        User user = getLoginUser();
        //解密oldPwd 并判断和用户密码对比
        if (!UserModel.staticEncodePwd(UserModel.staticDecryptPwd(oldPwd)).equals(user.getPassword())) {
            throw new BussinessException("当前密码输入有误，请确认后重新输入");
        }
        if (newPwd.equals(oldPwd)) {
            throw new BussinessException("新密码不能和旧密码相同");
        }
        //修改用户密码
        userService.updateByPrimaryKeySelective(new User(u -> {
            u.setId(user.getId());
            u.setPassword(UserModel.staticEncodePwd(UserModel.staticDecryptPwd(newPwd)));
        }));
        //删除缓存用户让用户重新登录
        delSession(SessionId.MEMBER_USER);
        return ok();
    }

    @ApiOperation(value = "个人中心-账号设置-获取登录用户", notes = "获取登录用户")
    @ResponseBody
    @RequestMapping(value = "loginuser", method = RequestMethod.POST)
    public ResultEntity<UserModel> loginUser() {
        User user = userService.selectByPrimaryKey(getLoginUser().getId());
        UserModel userModel = new UserModel();
        if (StrUtil.isNotBlank(user.getPayPassword())){userModel.setHasPayPwd(1);}
        userModel.setMobile(user.getMobile());
        userModel.setAvatarUrl(user.getAvatarUrl());
        userModel.setRealName(user.getRealName());
        userModel.setIdCard(user.getIdCard());
        return ok(userModel);
    }
}
